<script>
    Namespace.register("EBCMS.{$namespace}");
    $(function(){
        EBCMS.{$namespace}.queryParams = {
            page:1,
            rows:20,
            order:{
                'id':'desc',
            },
            model:'bulletin/bulletin',
        };
        EBCMS.{$namespace}.refresh = function refresh(p){
            if (p) {
                $.each(p, function(index, val) {
                    if (typeof val == 'object') {
                        EBCMS.{$namespace}.queryParams[index] = $.extend(EBCMS.{$namespace}.queryParams[index], p[index]);
                        delete p[index];
                    };
                });
                $.extend(EBCMS.{$namespace}.queryParams, p);
            };
            EBCMS.CORE.api({
                queryParams:EBCMS.{$namespace}.queryParams,
                tpl:'{$namespace}-table',
                target:'#{$namespace}_table',
                compileAfter:function(p){
                    EBCMS.FN.renderPage({
                        namespace:'{$namespace}',
                        total:p.data.total,
                    });
                    EBCMS.FN.renderFilter({
                        namespace:'{$namespace}',
                        filter:{
                            search:{
                                field:'title|body',
                                value:''
                            },
                            status:{
                                field:'status',
                                filters:{"已通过":1,'已屏蔽':0}
                            },
                            order:{
                                filters:{id:'id',update_time:'更新时间'}
                            },
                            rows:true,
                        }
                    });
                },
            });
        };
        EBCMS.{$namespace}.edit = function edit(id){
            EBCMS.CORE.get({
                url:'{:url('bulletin/bulletin/edit')}',
                queryParams:{
                    id:id,
                },
                target:'#lgModal .modal-content',
            });
        };
        EBCMS.{$namespace}.add = function add(pid){
            EBCMS.CORE.get({
                url:'{:url('bulletin/bulletin/add')}',
                queryParams:{
                    pid:pid,
                },
                target:'#lgModal .modal-content',
            });
        };
        EBCMS.{$namespace}.bold = function bold(id,value){
            EBCMS.CORE.submit({
                url:'{:url('bulletin/bulletin/style')}',
                queryParams:{
                    id: id,
                    bold:value
                },
                success:function(res){
                   if (!res.code) {
                       EBCMS.MSG.alert(res.msg);
                   }
                   EBCMS.{$namespace}.refresh();
                }
            });
        };
        EBCMS.{$namespace}.color = function color(id,value){
            EBCMS.CORE.submit({
                url:'{:url('bulletin/bulletin/style')}',
                queryParams:{
                    id: id,
                    color:value
                },
                success:function(res){
                   if (!res.code) {
                       EBCMS.MSG.alert(res.msg);
                   }
                   EBCMS.{$namespace}.refresh();
                }
            });
        };

        EBCMS.CORE.compile({
            tpl:'{$namespace}-box',
            target:'#{$namespace}_box',
            compileAfter:function(){
                EBCMS.{$namespace}.refresh();
            }
        });
    });
</script>

<script id="{$namespace}-box" type="text/html">
    <div class="header">
        <div class="header-title" onclick="EBCMS.{$namespace}.refresh();">公告管理</div>
        <div id="{$namespace}_filter" class="footer-page"></div>
        <div class="btn-group">
            <button class="btn btn-primary" onclick="EBCMS.CORE.getconfig('{:eb_encrypt('name|bulletin')}');">设置</button>
            <button class="btn btn-primary" onclick="EBCMS.{$namespace}.add();">添加</button>
        </div>
    </div>
    <div class="body">
        <div id="{$namespace}_table" class="box"></div>
    </div>
    <div class="footer">
        <div id="{$namespace}_page" class="footer-page"></div>
    </div>
</script>

<script id="{$namespace}-table" type="text/html">
    <table class="table table-bordered table-hover table-ebcms">
        <tbody>
            <tr>
                <th style="width:70px;">id</th>
                <th style="width:400px;">操作</th>
                <th>标题</th>
            </tr>
            [[include '{$namespace}-table-item' data]]
        </tbody>
    </table>
</script>

<script id="{$namespace}-table-item" type="text/html">
    [[each rows as v n]]
        <tr>
            <td>[[v.id]]</td>
            <td>
                <div class="btn-group btn-group-sm">
                    [[if v.status==1]]
                    <button class="btn btn-primary" onclick="EBCMS.ACT.status('{:url('bulletin/bulletin/status')}','[[v.id]]','0','{$namespace}');">已审</button>
                    [[else]]
                    <button class="btn btn-warning" onclick="EBCMS.ACT.status('{:url('bulletin/bulletin/status')}','[[v.id]]','1','{$namespace}');">未审</button>
                    [[/if]]
                    <button class="btn btn-primary" onclick="EBCMS.{$namespace}.edit('[[v.id]]');">编辑</button>
                    <button class="btn btn-primary" onclick="EBCMS.ACT.del('{:url('bulletin/bulletin/delete')}','[[v.id]]','{$namespace}');">删除</button>
                    <button class="btn btn-primary" onclick="EBCMS.CORE.recommend({
                        model:'bulletin/bulletin',
                        id:'[[v.id]]',
                        thumb:'[[v.thumb]]',
                        url:'[[v.ebcms_url]]',
                        title:'[[v.shorttitle | escape]]',
                        description:'[[v.description | escape]]'
                        });">推送</button>
                </div>
                <div class="btn-group btn-group-sm">
                    <div class="btn-group btn-group-sm dropup">
                      <button type="button" class="btn btn-primary" onclick="EBCMS.{$namespace}.bold('[[v.id]]','[[if '' == v.bold]]600[[/if]]');">加粗</button>
                      <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <span class="caret"></span>
                        <span class="sr-only">Toggle Dropdown</span>
                      </button>
                      <div class="dropdown-menu" style="padding:5px;">
                        <div class="btn-group" style="display: inline-flex;" role="group" aria-label="...">
                          <button type="button" class="btn btn-default" onclick="EBCMS.{$namespace}.bold('[[v.id]]','600');"><span style="font-weight:600;">加粗</span></button>
                          <button type="button" class="btn btn-default" onclick="EBCMS.{$namespace}.bold('[[v.id]]','900');"><span style="font-weight:900;">特粗</span></button>
                        </div>
                      </div>
                    </div>
                    <div class="btn-group btn-group-sm dropup">
                      <button type="button" class="btn btn-primary" onclick="EBCMS.{$namespace}.color('[[v.id]]','[[if '' == v.color]]#d9534f[[/if]]');">着色</button>
                      <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <span class="caret"></span>
                        <span class="sr-only">Toggle Dropdown</span>
                      </button>
                      <div class="dropdown-menu" style="padding:5px;">
                        <div class="btn-group" style="display: inline-flex;" role="group" aria-label="...">
                          <button type="button" class="btn btn-primary" onclick="EBCMS.{$namespace}.color('[[v.id]]','#428bca');">蓝</button>
                          <button type="button" class="btn btn-success" onclick="EBCMS.{$namespace}.color('[[v.id]]','#5cb85c');">绿</button>
                          <button type="button" class="btn btn-info" onclick="EBCMS.{$namespace}.color('[[v.id]]','#5bc0de');">青</button>
                          <button type="button" class="btn btn-warning" onclick="EBCMS.{$namespace}.color('[[v.id]]','#f0ad4e');">黄</button>
                          <button type="button" class="btn btn-danger" onclick="EBCMS.{$namespace}.color('[[v.id]]','#d9534f');">红</button>
                        </div>
                      </div>
                    </div>
                </div>
            </td>
            <td><a href="{:url('index/api/gourl')}?url=[[encodeURIComponent('index/bulletin/detail?id=')]][[v.id]]" target="_blank"><span style="[[if v.color]]color:[[v.color]];[[/if]][[if v.bold]]font-weight:[[v.bold]];[[/if]][[if v.size>10]]font-size:[[v.size]]px;[[/if]]">[[v.title]]</span></a></td>
        </tr>
    [[/each]]
</script>